package com.packet.controller;

import com.packet.pojo.*;
import com.packet.service.ItemService;
import com.packet.service.StaffCustomsService;
import com.packet.service.StaffExamineService;
import com.packet.service.StaffTripService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @ClassName:StaffTripController
 * @Author: mu
 * @Date:2023-12-05 16:51
 * @Describe:
 */
@Controller
public class StaffTripController {
    @Autowired
    private StaffTripService staffTripService;
    @Autowired
    private StaffCustomsService staffCustomsService;
    @Autowired
    private StaffExamineService staffExamineService;

    @RequestMapping("/staff/oldList")
    public String getOldTrip(Model model,HttpSession session){
        Integer sid = (Integer) session.getAttribute("sid");
        List<Trip> oldTrip = staffTripService.getOldTrip(sid);
        model.addAttribute("staffOldTrip",oldTrip);
        return "staff/Trip/oldList";
    }

    @RequestMapping("/staff/detailTrip/{bid}")
    public String getDetailTrip(@PathVariable("bid") Integer bid,Model model){
        Trip detailTrip = staffTripService.getDetailTrip(bid);
        List<Customs> customs = staffCustomsService.getCustoms(bid);
        List<Customs> customsThen = staffCustomsService.getCustomsThen(bid);
        model.addAttribute("detailTrip",detailTrip);
        model.addAttribute("detailCustoms",customs);
        model.addAttribute("detailCustomsThen",customsThen);
        return "staff/Trip/detailTrip";
    }

    @RequestMapping("/staff/doingList")
    public String getDoingTrip(Model model,HttpSession session){
        Integer sid = (Integer) session.getAttribute("sid");
        List<Trip> doingTrip = staffTripService.getDoingTrip(sid);
        model.addAttribute("staffDoingTrip",doingTrip);
        return "staff/Trip/doingList";
    }

    @PostMapping("/staff/addTrip")
    public String addTrip(Trip trip,HttpSession session,Integer manageId, Integer advence){
        Integer sid = (Integer) session.getAttribute("sid");
        //添加行程表
        trip.setSId(sid);
        trip.setState("0");
        staffTripService.addTrip(trip);
        //添加审批表
        Examine examine = new Examine();
        examine.setSid(manageId);
        examine.setBid(staffTripService.getNewTripBid(sid));
        staffExamineService.addExamine(examine);
        //判断是否继续进行预支款
        if (advence == 0){
            return "redirect:/staff/doingList";
        }else{
            return "staff/customs/addAdvence";
        }

    }

    @RequestMapping("/staff/getManage")
    public String getManage(Model model){
        List<Staff> manage = staffTripService.getManage();
        model.addAttribute("manage",manage);
        return "staff/Trip/addTrip";
    }

    @RequestMapping("/staff/deleteTrip/{bid}")
    public String deleteTrip(@PathVariable("bid") Integer bid){
        staffExamineService.deleteExamine(bid);
        staffTripService.delectTrip(bid);
        staffCustomsService.deleteCustoms(bid);
        return "redirect:/staff/doingList";
    }

    @RequestMapping("/staff/updateGetTrip/{bid}")
    public String updateGetTrip(@PathVariable("bid") Integer bid,Model model){
        Trip detailTrip = staffTripService.getDetailTrip(bid);
        List<Staff> manage = staffTripService.getManage();
        Examine examine = staffExamineService.getExamineByBid(bid);
        model.addAttribute("detailTrip",detailTrip);
        model.addAttribute("manage",manage);
        model.addAttribute("ExamineSid",examine.getSid());
        System.out.println(detailTrip);
        return "/staff/Trip/updateTrip";
    }

    @RequestMapping("/staff/updateTrip/{bid}")
    public String updateTrip(Trip trip,@PathVariable("bid") Integer bid,HttpSession session){
        Integer sid = (Integer)session.getAttribute("sid");
        trip.setSId(sid);
        trip.setBId(bid);
        trip.setState("0");
        System.out.println(trip);
        staffTripService.updateTrip(trip);
        return "redirect:/staff/doingList";
    }

    @RequestMapping("/staff/completeTrip/{bid}")
    public String completeTrip(@PathVariable("bid") Integer bid){
        Trip completeTrip = staffTripService.getDetailTrip(bid);
        completeTrip.setState("3");
        staffTripService.updateTrip(completeTrip);
        System.out.println(completeTrip);
        return "redirect:/staff/doingList";
    }

}
